<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<%@ include file="../inc/fileHead.jsp"%>
<body>
	<div>
		选择用户： <input class="easyui-combobox" id="salesId"
			data-options="  
                    url:'BaseAction_selectGetSalesName',  
                    valueField:'id',  
                    textField:'name',  
                    panelHeight:'auto'">
	</div>
	<div>
		客户级别： <input  class="easyui-combobox"
				name="customerlevel"
				data-options="  
                    url:'BaseAction_selectGetCustomerLevel',  
                    valueField:'id',  
                    textField:'name',  
                    panelHeight:'auto',
                    onSelect:function(rec){
                    var url = 'BaseAction_selectGetCustomerNameByLevel?customerlevel='+rec.id; 
                     $('#customerId').combobox('reload', url); 
                    }
            " />
     </div>
     <div>
			客户名称： <input id="customerId"  class="easyui-combobox"
				name="customerId"
				data-options="  
                    valueField:'id',  
                    textField:'name',  
                    panelHeight:'auto'  
            " />
		<input type="button" value="添加到管理辖区" onclick="add()" /> <input
			type="button" value="查看管理辖区" onclick="search()" />
	</div>
	<div>
		<table id="dg" style="width: 700px; height: 250px"></table>
	</div>
	<script type="text/javascript">
		function add() {
			$.ajax({
				type : "get",
				url : "UserCustomerRelationAction_addRelation",
				data : "salesId=" + $("#salesId").combobox("getValue")
						+ "&customerId="
						+ $("#customerId").combobox("getValue"),//提交表单，相当于CheckCorpID.ashx?ID=XXX
				success : function(result) {
				var result = eval('('+result+')');  
					if(result.success){
						$.messager.alert('提示','操作成功'); 
						search();
					}else{
						$.messager.alert('提示','操作失败'); 
					}
					
				} //操作成功后的操作！msg是后台传过来的值
			});
		}

		function search() {
			$('#dg').datagrid({
				toolbar : [ {
					iconCls : 'icon-remove',
					text:'删除',
					handler : remove
				} ],
				singleSelect : "true",
				url : 'UserCustomerRelationAction_findAllDistrictBySales',
				queryParams : {
					salesId : $('#salesId').combobox("getValue")
				},
				columns : [ [ {
					field : 'salesname',
					title : 'sales名称',
					width : 200
				}, {
					field : 'customername',
					title : '客户名称',
					width : 300
				}, ] ]
			});
		}

		function remove() {
			var row = $('#dg').datagrid('getSelected');
			if (row) {
				$.messager.confirm('Confirm',
						'确认删除该记录?',
						function(r) {
							if (r) {
								$.post('UserCustomerRelationAction_deleteRelation', {
									salesId : row.salesId,
									customerId : row.customerId
								}, function(result) {
									if (result.success) {
										$('#dg').datagrid('reload'); // reload the user data  
									} else {
										$.messager.alert('错误',result.errorMsg); 
									}
								}, 'json');
							}
						});
			}
		}
	</script>
</body>
</html>